<template>
  <div class="background">
    <el-row :gutter="20">
      <el-col :span="4"></el-col>
      <el-col :span="16">
        <el-card class="box-card">
          <template #header>
            <div class="card-header">
              <div>
                <span class="title">{{ order.title }}</span>
                <span class="type">{{ order.type }}</span>
              </div>

              <button
                class="button"
                style="vertical-align: middle"
                @click="takeOrder(order_id)"
              >
                <span>我要接单</span>
              </button>
            </div>
          </template>
          <div class="rightInfo">
            <div class="rightOwner">
              <div class="rightOwnerLeft">
                <el-avatar
                  class="avatar"
                  :size="60"
                  :src="getUserAvatar(order.customer_id)"
                />
                <!-- 这里要显示的是发单人的一些信息 -->
                <div class="rightOwnerInfo">
                  <span class="">{{ customer.nick_name }}</span>
                  <span class="little">{{ customer.age }}岁</span>

                  <span class="little"
                    >信誉分：{{ customer.belief_score }}</span
                  >
                  <span class="little">{{ customer.gender }}</span>
                </div>
              </div>
              <button
                class="button"
                style="vertical-align: middle"
                @click="toUserInfo"
              >
                <span>查看名片 </span>
              </button>
              <!-- 此处增加butten，跳转至名片详情页面 -->
            </div>

            <!-- 【！要求！】 -->
            <!--  -->
            <div class="details">
              <h3 class="subtitle">需求描述：</h3>
              <p>{{ order.description }}</p>
              <h3 class="subtitle">工作时间：</h3>
              <p>工作时间：{{ order.start_time }}</p>
              <p>工作时长：{{ order.dureation }}</p>
              <h3 class="subtitle">工作地点：</h3>
              <div>
                <new_map
                  :local_latitude="order.latitude"
                  :local_longitude="order.longitude"
                ></new_map>
              </div>
              <h3 class="subtitle">接单要求：</h3>
              <p>性别：{{ order.gender_requirement }}</p>
              <p>人群：{{ order.requirement }}</p>
              <p>其他：{{ order.demand }}</p>
            </div>
          </div>
        </el-card></el-col
      >
      <el-col :span="4"></el-col>
    </el-row>
  </div>
</template>
<script>
// import main_card from "@/components/main_card.vue";
import new_map from "@/components/new_map.vue";
import { useStore } from "vuex";
import { ElNotification } from "element-plus";

export default {
  computed: {
    userId() {
      const store = useStore();
      return store.state.user_id;
    },
  },
  components: {
    new_map,
  },
  data() {
    return {
      user_data: [],
      order_id: "",
      order: {
        title: "",
        customer_id: "",
        duration: "",
        type: "",
        description: "无",
        release_time: "",
        date: "",
        start_time: "",
        end_time: "",
        latitude: "39",
        longitude: "117",
        gender_requirement: "",
        requirement: "",
      },
      customer: {
        nick_name: "",
        gender: "",
        age: "",
        belief_score: "",
      },
    };
  },
  methods: {
    handleChange() {
      const store = useStore();
      const newUserId = "6";
      store.dispatch("change", newUserId);
    },
    requestAvatar() {
      this.axios.post("http://127.0.0.1:8000/userInfos/").then((res) => {
        if (res.data.success) {
          this.user_data = res.data.data;
        }
      });
    },
    getUserAvatar(customer_id) {
      for (let item of this.user_data) {
        if (item.id == customer_id) {
          return item.avatar;
        }
      }
    },
    get_details() {
      const params = new URLSearchParams();
      params.append("order_id", this.order_id);
      this.axios
        .post("http://127.0.0.1:8000/get_details/", params)
        .then((res) => {
          if (res.data.success) {
            this.order = res.data.data;
            console.log(this.order.customer_id);
            this.get_customer();
          }
        })
        .catch((error) => {
          console.error(error); // 输出错误信息，以便调试
        });
    },
    get_customer() {
      const params = new URLSearchParams();
      params.append("customer_id", this.order.customer_id);
      this.axios
        .post("http://127.0.0.1:8000/get_customer/", params)
        .then((res) => {
          if (res.data.success) {
            this.customer = res.data.data;
            console.log(this.customer);
          }
        })
        .catch((error) => {
          console.error(error); // 输出错误信息，以便调试
        });
    },
    toUserInfo() {
      this.$router.push({
        path: "/UserInfo",
        query: {
          id: this.order.customer_id,
          order_id: this.order_id,
        },
      });
    },
    takeOrder(id) {
      const params = new URLSearchParams();
      console.log("item.id:", id);
      params.append("order_id", id);
      this.axios
        .post("http://127.0.0.1:8000/take_order/", params)
        .then((res) => {
          if (!res.data.success) {
            ElNotification({
              title: res.data.title,
              message: res.data.message,
              type: "error",
            });
          } else {
            ElNotification({
              title: res.data.title,
              message: res.data.message,
              type: "success",
            });
          }
        });
    },
  },
  mounted() {
    this.order_id = this.$route.query.order_id;
    console.log("来自：", this.order_id);
    this.get_details();
    this.requestAvatar();
  },
};
</script>
<style scoped>
.background {
  background: linear-gradient(rgba(211, 194, 246, 0.795), white);
  font-family: sans-serif;
}
.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 20px;
}

.title {
  font-size: 20px;
  font-family: "Gill Sans", "Gill Sans MT", Calibri, "Trebuchet MS", sans-serif;
  letter-spacing: 2px;
  font-weight: bolder;
}

.type {
  padding: 5px 13px;
  background-color: lavender;
  border-radius: 10px;
  margin-left: 13px;
  max-height: 16px;
  flex-shrink: 0;
  font-size: 15px;
  font-weight: bold;
}
.box-card {
  width: 100%;
  margin: 50px 0;
}

.grid-content {
  border-radius: 4px;
  min-height: 36px;
  background-color: red;
}
/* 发单人 */

.little {
  color: gray;
  font-size: 15px;
}
.rightOwner {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}
.rightOwnerLeft {
  display: flex;
  justify-content: flex-start;
  align-items: center;
}
.rightOwnerInfo {
  display: flex;
  padding: 10px;
}
.rightOwnerInfo span {
  margin: 0 10px;
}
.rightInfo {
  padding: 3px 20px;
}
.button {
  display: inline-block;
  border-radius: 4px;
  background-color: #666699;
  border: none;
  color: #ffffff;
  text-align: center;
  font-size: 13px;
  padding: 10px;
  width: 100px;
  transition: all 0.5s;
  cursor: pointer;
  margin: 5px;
}

.button span {
  cursor: pointer;
  display: inline-block;
  position: relative;
  transition: 0.5s;
}

.button span:after {
  content: "»";
  position: absolute;
  opacity: 0;
  top: 0;
  right: -20px;
  transition: 0.5s;
}

.button:hover span {
  padding-right: 25px;
}

.button:hover span:after {
  opacity: 1;
  right: 0;
}
/* 订单信息 */
.subtitle {
  margin: 16px 0;
  font-size: 16px;
  font-weight: 600;
  color: #222;
  line-height: 22px;
}

.details p {
  font-size: 14px;
  color: #222;
  line-height: 24px;
  text-align: justify;
  white-space: pre-wrap;
  word-break: break-all;
}
</style>
